<template>
  <div class="setting-body">
    <el-form ref="form" :model="form" size="small" label-width="120px">
      <div class="custom-tips">
        基础设置
      </div>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="是否评价" prop="isEvaluate" :rules="[{ required: true, message: '请选择是否评价', trigger: 'change' }]">
            <el-radio-group v-model="form.isEvaluate" style="width: 200px">
              <el-radio
                v-for="item in status"
                :key="item.id"
                :label="item.value"
              >{{ item.label }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="是否咨询" prop="counsel" :rules="[{ required: true, message: '请选择是否咨询', trigger: 'change' }]">
            <el-radio-group v-model="form.counsel" style="width: 200px">
              <el-radio
                v-for="item in status"
                :key="item.id"
                :label="item.value"
              >{{ item.label }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
      <div class="custom-tips">
        统计项设置
        <div class="button-add">
          <el-button type="text" icon="el-icon-plus" @click="doAdd">新增</el-button>
        </div>
      </div>
      <el-row :gutter="20" style="margin-top: 10px">
        <draggable v-model="form.data" animation="300" @end="clone">
          <el-col v-for="(item,index) in form.data" :key="'data-'+index" style="border-bottom: 1px solid #e2e2e2;margin-bottom: 10px" :span="24">
            <el-row :gutter="10">
              <el-col :span="12">
                <el-form-item
                  label="显示标题"
                  :prop="'data.'+index+'.label'"
                  :rules="[{ required: true, message: '请输入显示标题', trigger: 'blur' }]"
                  style="width: 100%"
                >
                  <el-input v-model="item.label" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item
                  label="字体颜色"
                  :prop="'data.'+index+'.titleColor'"
                  :rules="[{ required: true, message: '请选择字体颜色', trigger: 'change' }]"
                  style="width: 100%"
                >
                  <el-color-picker v-model="item.titleColor" />
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item
                  label="数据来源"
                  :prop="'data.'+index+'.sjly'"
                  style="width: 100%"
                >
                  <el-popover
                    placement="bottom-start"
                    width="450"
                    trigger="click"
                  >
                    <notice-data ref="tempSelect" v-model="item.sjly" />
                    <el-input slot="reference" v-model="item.sjly.name" style="width: 450px" placeholder="点击选择模版" readonly />
                  </el-popover>
                </el-form-item>
              </el-col>
              <el-col v-if="item.sjly.jxx" :span="24">
                <div class="custom-tips">
                  参数设置
                  <div class="button-add">
                    <el-button type="text" icon="el-icon-plus" @click="doAddParams(item)">新增</el-button>
                  </div>
                  <div v-if="item.params">
                    <el-row v-for="(obj,i) in item.params" :key="'code-'+i" :gutter="20" style="margin-top: 10px">
                      <el-col :span="10">
                        <el-form-item
                          label="参数名称"
                          style="width: 100%"
                        >
                          <el-input v-model="obj.label" />
                        </el-form-item>
                      </el-col>
                      <el-col :span="10">
                        <el-form-item
                          label="数据项"
                        >
                          <el-select v-model="obj.code" clearable placeholder="请选择数据项">
                            <el-option
                              v-for="obj in item.sjly.jxx"
                              :key="obj"
                              :label="obj"
                              :value="obj"
                            />
                          </el-select>
                        </el-form-item>
                      </el-col>
                      <el-col :span="4" style="text-align: center">
                        <el-button type="text" icon="el-icon-delete" size="mini" @click="toDeleteParams(item,obj)">删除</el-button>
                      </el-col>
                    </el-row>
                  </div>
                </div>
              </el-col>
              <el-col :span="24">
                <el-form-item label="展示配置" :prop="'data.'+index+'.show'">
                  <wang-editor v-model="item.show" :is-clear="false" />
                </el-form-item>
              </el-col>
              <el-col :span="20">&nbsp;</el-col>
              <el-col :span="4" style="text-align: center">
                <el-button type="text" icon="el-icon-delete" size="mini" @click="toDelete(item)">删除</el-button>
              </el-col>
            </el-row>
          </el-col>
        </draggable>
      </el-row>
    </el-form>
    <div class="custom-button">
      <el-button type="text" @click="close">取消</el-button>
      <el-button :loading="loading" type="primary" @click="save('form')">确认</el-button>
    </div>
  </div>
</template>

<script>
import { updateObject } from '@/api/portal/manager/entityTemp';
import draggable from 'vuedraggable';
import NoticeData from '@/components/NoticeData';
import WangEditor from '@/components/wangEdit';
export default {
  name: 'ServerEntityV1',
  components: { draggable, NoticeData, WangEditor },
  props: {
    column: {
      type: Object,
      default: () => {
        return {};
      }
    }
  },
  data() {
    return {
      loading: false,
      status: [
        { id: 1, label: '否', value: false },
        { id: 2, label: '是', value: true }
      ],
      dataTypes: [],
      baseURL: process.env.VUE_APP_BASE_API, // api 的 base_url
      form: {
        isEvaluate: false, // 是否评价
        counsel: false, // 是否显示咨询
        data: []
      }
    };
  },
  watch: {
    column: {
      handler(newName, oldName) {
        if (undefined !== newName.pzx && newName.pzx !== null) {
          this.form = JSON.parse(newName.pzx);
        }
      },
      immediate: true, // 父组件第一次传值时，执行handler，false则第一次不执行
      deep: false // formType为对象时，设置为true，此属性监听对象发生变化
    }
  },
  created() {
    // 解决火狐拖拽搜索的问题
    document.body.ondrop = function(event) {
      event.preventDefault();
      event.stopPropagation();
    };
  },
  methods: {
    doAdd() {
      const item = { label: '', sjly: {}, px: 0, titleColor: '', params: [], show: '' };
      this.form.data.push(item);
    },
    doAddParams(data) {
      console.log(data);
      if (data.params === undefined || data.params === null) {
        data.params = [];
      }
      const item = { label: '', code: '' };
      data.params.push(item);
      this.$forceUpdate();
    },
    toDelete(item) {
      const index = this.form.data.indexOf(item);
      if (index !== -1) {
        this.form.data.splice(index, 1);
      }
    },
    toDeleteParams(data, item) {
      const index = data.params.indexOf(item);
      if (index !== -1) {
        data.params.splice(item, 1);
      }
    },
    clone() {
      this.form.data = this.form.data.map((item, index) => {
        item.px = index + 1;
        return item;
      });
    },
    save(formName) {
      this.loading = true;
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.column.pzx = JSON.stringify(this.form);
          updateObject(this.column).then(res => {
            this.$message.success('更新成功！');
            this.close();
          }).catch(() => {
            this.loading = false;
            this.$message.error('更新失败！');
          });
        } else {
          this.loading = false;
          console.log('error submit!!');
          return false;
        }
      });
    },
    close() {
      this.loading = false;
      Object.assign(this.$data.form, this.$options.data.call(this).form);
      this.dialogFormVisible = false;
      this.$emit('fetch-data');
    }
  }
};
</script>

<style scoped>
.setting-body {
  width: 100%;
}
.custom-tips {
  width: 100%;
  min-height: 40px;
  line-height: 40px;
  text-align: left;
  padding: 8px 16px;
  background-color: #ecf8ff;
  border-radius: 4px;
  border-left: 5px solid #50bfff;
}
.button-add {
  width: 50px;
  height: 40px;
  float: right;
  text-align: center;
  line-height: 40px;
  margin-right: 20px;
}
.custom-button {
  text-align: right;
}
</style>
